import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

/*
 * Emergency minigame
 * 
 * Goal of the game is to take every call of emergency. And than send firemen to the problem.
 * When the player takes the telephone, the problem will be shown. 
 * @author Jonathan van Es
 */
public class Office extends MiniGame
{

    private List<Desk> desks ;
    private int totalezwaarte = 0;
    private Bar bar;
    public GreenfootSound music;
    private calamiteit c;

    public Bar getBar(){
        return this.bar;
    }


    public Office()
    {    

        super(240, 1); 
        

        desks = new ArrayList<Desk>();
        //add the objects
        Desk Desk1 = new Desk();
        addObject(Desk1, 50, 50);
        Desk1.start();

        Desk Desk2 = new Desk();
        addObject(Desk2, 300, 50); 
        Desk2.start();

        Desk Desk3 = new Desk();
        addObject(Desk3, 550, 50); 
        Desk3.start();

        Desk Desk4 = new Desk();
        addObject(Desk4, 50, 350); 
        Desk4.start();

        Desk Desk5 = new Desk();
        addObject(Desk5, 300, 350); 
        Desk5.start();

        Desk Desk6 = new Desk();
        addObject(Desk6, 550, 350); 
        Desk6.start();

        desks.add(Desk1);
        desks.add(Desk2);
        desks.add(Desk3);
        desks.add(Desk4);
        desks.add(Desk5);
        desks.add(Desk6);

        
        addObject(new person(), 300, 200);
        setPaintOrder(Telephone.class, Desk.class);

        bar = new Bar("Brandweermannen", "stuks", 30, 30);
        addObject(bar, 400, 383);
        music = new GreenfootSound("ringtelephone.wav"); 

    }
    private long timeStarted = System.currentTimeMillis(); 
    /*
     * While the game is in GameStatus.PLAYING checks to see if Calamiteit exists. If not, the office makes automaticly a Calamiteit and plays the sound.
     */
    public void act(){
        if(getGameStatus() == GameStatus.PLAYING)
        {
            long currentTime = System.currentTimeMillis();
            long elapsedTime = currentTime - timeStarted;
            if(elapsedTime / 1000 >= 5 )
            {
                int random = Greenfoot.getRandomNumber(6);

                Desk desk = desks.get(random);

                if(desk.getCalamiteit() == null)
                {
                    calamiteit c = new calamiteit(this.bar, this, desk);
                    addObject(c,1,1);
                    desk.setCalamiteit(c);
                    play();                
                }

                timeStarted = System.currentTimeMillis();
            }


            // if the score is higher than 10000, you have won the game
            if(getScore() >10000){
                super.gameWon(); 
            }
        } 
        
        super.act();
    }

    public void stop(){        
        music.stop();
    }

    public void play(){        
        music.play();
    }
}
